/*
 * ECARX Technology Limited is the owner of the copyright and the trade secret of this software.
 * Without permission, no one has the right to obtain, disclose or use this software in any way.
 */

package com.ecarx.systemui.plugin.ui.custom;

import android.content.Context;
import android.util.AttributeSet;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.TextView;

import androidx.annotation.Nullable;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.ecarx.module_bluetooth.aosp.CachedBluetoothDevice;
import com.ecarx.module_bluetooth.base.enums.SwitchStateEnum;
import com.ecarx.systemui.plugin.R;
import com.ecarx.systemui.plugin.controller.BluetoothController;
import com.ecarx.systemui.plugin.ui.adapter.BtDevicesAdapter;
import com.ecarx.systemui.plugin.ui.adapter.entity.ItemBluetooth;
import com.ecarx.systemui.plugin.ui.adapter.entity.ItemBluetoothConnected;
import com.ecarx.systemui.plugin.ui.adapter.entity.ItemScanBluetooth;
import com.ecarx.systemui.plugin.ui.adapter.entity.ItemTextLabelBluetooth;
import com.ecarx.systemui.plugin.ui.dialog.base.DialogManager;
import com.ecarx.systemui.plugin.utils.AppUtils;
import com.ecarx.systemui.plugin.utils.LogUtils;

import java.util.ArrayList;
import java.util.List;


public class BluetoothListView extends FrameLayout {

    private static final String TAG = "BluetoothListView";
    private RecyclerView rvBt;
    private View btHandleGroup;
    private BluetoothController mBtController;
    private final List<ItemBluetooth> mDeviceItemList = new ArrayList<>();
    private BtDevicesAdapter mBtDevicesAdapter;

    public BluetoothListView(Context context) {
        this(context, null);
    }

    public BluetoothListView(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public BluetoothListView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    private void updateBtName(String btName){
        TextView tvBtName = findViewById(R.id.tv_label_desc);
        tvBtName.setText(String.format(getResources().getString(R.string.bt_name_placeholder), btName));
    }

    private void init() {
        LayoutInflater.from(getContext()).inflate(R.layout.cust_view_layout_bluetooth,
                this, true);

        mBtController = BluetoothController.getInstance();

        CustomSwitch btSwitch = findViewById(R.id.bt_switch);
        CustomSwitch btHandleSwitch = findViewById(R.id.sw_handle);
        btHandleSwitch.uiOpen();
        updateBtName(BluetoothController.getInstance().getBtName());

        btHandleGroup = findViewById(R.id.bt_handle_group);
        btHandleGroup.setVisibility(View.GONE);

        rvBt = findViewById(R.id.rv_bt);
        rvBt.setLayoutManager(new LinearLayoutManager(getContext()));

        TextView tvBtPreferences = findViewById(R.id.tv_bt_preferences);
        tvBtPreferences.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                DialogManager.getDialogManager().dismissBtWifiDialog();
                AppUtils.openBtSettings(getContext());
            }
        });
        mBtController.registerBTStateListener(new BluetoothController.OnBTStateListener() {
            @Override
            public void onBtState(SwitchStateEnum switchStateEnum) {
                Log.d(TAG, "onBtState: " + switchStateEnum);
                switch (switchStateEnum){
                    case ON:
                        btSwitch.uiOpen();
                        updateDeviceList();

                        break;
                    case OFF:
                        btSwitch.uiClose();
                        hideRecyclerView();
                        btHandleGroup.setVisibility(GONE);
                        mBtController.stopScanMode();
                        break;
                }
            }

            @Override
            public void onBtName(String name) {
                updateBtName(name);
            }

            @Override
            public void onScanDevices(List<CachedBluetoothDevice> scanDevices) {
                for (CachedBluetoothDevice scanDevice : scanDevices) {
                    ItemScanBluetooth itemScanBluetooth = new ItemScanBluetooth(scanDevice);
                    if (!mDeviceItemList.contains(itemScanBluetooth)) {
                        mDeviceItemList.add(itemScanBluetooth);
                    }
                }
                mBtDevicesAdapter.notifyDataSetChanged();
                LogUtils.d(TAG, "onScanDevices: notifyDataSetChanged " + mDeviceItemList);
            }

            @Override
            public void onScanStateChanged(boolean start) {
                // true 显示loading，false 隐藏loading
                mBtDevicesAdapter.onScanStateChanged(start);
            }
        });
        btSwitch.setSwitchChangeListener(new CustomSwitch.SwitchChangeListener() {
            @Override
            public void toClose() {
                mBtController.closeBlue();
                LogUtils.d(TAG, "btSwitch toClose: ");
            }

            @Override
            public void toOpen() {
                mBtController.openBlue();
                LogUtils.d(TAG, "btSwitch toOpen: ");
            }
        });

        mDeviceItemList.clear();
        mDeviceItemList.add(new ItemTextLabelBluetooth(true));

        if (mBtController.isEnable()) {
            btSwitch.uiOpen();
            updateDeviceList();
        } else {
            btSwitch.uiClose();
        }

        mBtDevicesAdapter = new BtDevicesAdapter(mDeviceItemList);
        rvBt.setAdapter(mBtDevicesAdapter);
    }

    private void updateDeviceList() {
        List<CachedBluetoothDevice> pairedBtDevices = mBtController.getPairedBtDevices();
        Log.d(TAG, "init: showDevices " + pairedBtDevices.size());
        for (CachedBluetoothDevice pairedBtDevice : pairedBtDevices) {
            mDeviceItemList.add(new ItemBluetoothConnected(pairedBtDevice));
        }
        mDeviceItemList.add(new ItemTextLabelBluetooth());
        mBtController.startScanMode();
        showRecyclerView();
        btHandleGroup.setVisibility(VISIBLE);
    }

    private void showRecyclerView() {
        if (rvBt != null) {
            rvBt.setVisibility(VISIBLE);
        }
    }

    private void hideRecyclerView() {
        if (rvBt != null) {
            rvBt.setVisibility(GONE);
        }
    }


}
